//
//  EditProfileGamesViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/16.
//

import UIKit

class EditProfileGamesViewController: UIViewController,CustomNavgationBar{
    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    var valorantItem: UIButton!
    var lolItem: UIButton!
    var overwatchItem: UIButton!
    var wowItem: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.createXglView()
        self.creatClearNavigationBar(title: "Edit Profile")
    }

}

extension EditProfileGamesViewController{
    @objc private func skipAction(){
//        self.dismiss(animated: true)
        if let window = UIApplication.shared.currentWindow {
            window.rootViewController = MainViewController
        }
    }
    
    @objc private func gameItemAction(sender: UIButton){
        sender.isSelected = !sender.isSelected
        if sender.isSelected{
            sender.layerBorderWidth = 1.0
        }else{
            sender.layerBorderWidth = 0.0
        }
    }
    
    @objc private func returnAction(){
        self.navigationController?.popViewController()
    }
    
    @objc private func nextAction(){
        var games: [String] = []
        if self.valorantItem.isSelected{
            games.append("1")
        }
        if self.lolItem.isSelected{
            games.append("2")
        }
        if self.overwatchItem.isSelected{
            games.append("3")
        }
        if self.wowItem.isSelected{
            games.append("4")
        }
        LoginInfor.share.infor?.games = games
        self.completeInforRequest()
    }
    
}

extension EditProfileGamesViewController{
    private func completeInforRequest(){
        guard let infor = LoginInfor.share.infor else { return }
        self.hudView.hudView.startAnimation()
        self.hudView.hudView.backgroundColor = .clear
        self.view.addSubview(self.hudView)
        self.hudView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.top.equalTo(PanNavibarH)
        }
        PanRequestManager<XGL_RequestBaseResponse<CompleteInfor>,XGL_CustomRequestError>().request(LoginRequestAPI(requestAPI: .CompleteInfor(infor: infor, param1: randomString(), param2: randomString()))) { result in
            self.hudView.dismiss()
            switch result{
            case .success(let response):
                if let result = response.resultData {
                    LoginInfor.share.infor?.treeId = result.id
                    LoginInfor.share.infor?.profileStatus = result.profileStatus
                    XGL_Notice.default.post(name: XGL_Notification.login.notificationName, object: nil)
                }
//                self.dismiss(animated: true)
                if let window = UIApplication.shared.currentWindow {
                    window.rootViewController = MainViewController
                }
                break
            default:
                break
            }
        }
    }
}

extension EditProfileGamesViewController{
    
    private func createXglView(){
        let titleText = UILabel().then { make in
            make.text = "Gender"
            make.textColor = .white
            make.font = UIFont.pingfangSC_Semibold(fontSize: 34.0)
        }
        self.valorantItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_game_valorant"), for: .normal)
            make.layerCornerRadius = 12.0
            make.clipsToBounds = true
            make.layerBorderColor = UIColor.hexColor("#80FFE6")
            make.layerBorderWidth = 0
            make.addTarget(self, action: #selector(self.gameItemAction(sender:)), for: .touchUpInside)
        })
        self.lolItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_game_lol"), for: .normal)
            make.layerCornerRadius = 12.0
            make.clipsToBounds = true
            make.layerBorderColor = UIColor.hexColor("#80FFE6")
            make.layerBorderWidth = 0
            make.addTarget(self, action: #selector(self.gameItemAction(sender:)), for: .touchUpInside)
        })
        self.overwatchItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_game_overwatch"), for: .normal)
            make.layerCornerRadius = 12.0
            make.clipsToBounds = true
            make.layerBorderColor = UIColor.hexColor("#80FFE6")
            make.layerBorderWidth = 0
            make.addTarget(self, action: #selector(self.gameItemAction(sender:)), for: .touchUpInside)
        })
        self.wowItem = UIButton().then({ make in
            make.setImage(imageWithName("icon_game_wow"), for: .normal)
            make.layerCornerRadius = 12.0
            make.clipsToBounds = true
            make.layerBorderColor = UIColor.hexColor("#80FFE6")
            make.layerBorderWidth = 0
            make.addTarget(self, action: #selector(self.gameItemAction(sender:)), for: .touchUpInside)
        })
        let returnButton = UIButton().then { make in
            make.setTitle("Return", for: .normal)
            make.setTitleColor(UIColor.hexColor("#80FFE6"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6").withAlphaComponent(0.1)
            make.layerCornerRadius = 20.0
            make.addTarget(self, action: #selector(self.returnAction), for: .touchUpInside)
        }
        let sureButton = UIButton().then { make in
            make.setTitle("Confirm", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 20.0
            make.addTarget(self, action: #selector(self.nextAction), for: .touchUpInside)
        }
        self.view.addSubviews([titleText,valorantItem,lolItem,overwatchItem,wowItem,returnButton,sureButton])
        titleText.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 188.0))
        }
        valorantItem.snp.makeConstraints { make in
            make.right.equalTo(lolItem.snp.left).offset(-conversionWithScrennWidth(value: 40.0))
            make.centerY.height.width.equalTo(lolItem)
        }
        lolItem.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleText.snp.bottom).offset(conversionWithScrennWidth(value: 56.0))
            make.width.height.equalTo(conversionWithScrennWidth(value: 64.0))
        }
        overwatchItem.snp.makeConstraints { make in
            make.centerY.height.width.equalTo(lolItem)
            make.left.equalTo(lolItem.snp.right).offset(conversionWithScrennWidth(value: 40.0))
        }
        wowItem.snp.makeConstraints { make in
            make.centerX.width.height.equalTo(valorantItem)
            make.top.equalTo(valorantItem.snp.bottom).offset(conversionWithScrennWidth(value: 40.0))
        }
        sureButton.snp.makeConstraints { make in
            make.left.equalTo(self.view.snp.centerX).offset(conversionWithScrennWidth(value: 5.0))
            make.width.equalTo(conversionWithScrennWidth(value: 168.0))
            make.height.equalTo(48.0)
            make.bottom.equalTo(-BottomHeight-48.0)
        }
        returnButton.snp.makeConstraints { make in
            make.centerY.height.width.equalTo(sureButton)
            make.right.equalTo(sureButton.snp.left).offset(-10.0)
        }
    }
    
    func creatClearNavigationBar(title: String) {
        self.navigationController?.navigationBar.barStyle = .default
        setNeedsStatusBarAppearanceUpdate()
        let backColor = UIColor.clear
        let imageIcon = imageWithName("icon_xgl_back_white")
       
        let naviView = UIView().then { make in
            make.backgroundColor = backColor
        }
        let backItem = MXCustomButton().then { make in
            make.setImage(imageIcon, for: .normal)
            make.buttonSelectAction { [weak self] in
                guard let `self` = self else { return }
                self.backAction()
            }
        }
        let titleView = UILabel().then { make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.text = title
        }
        let skipItem = UIButton().then { make in
            make.addTarget(self, action: #selector(self.skipAction), for: .touchUpInside)
            make.setTitle("Skip", for: .normal)
            make.setTitleColor(UIColor.hexColor("#80FFE6"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.backgroundColor = UIColor.hexColor("#001310")
            make.layerCornerRadius = 12.0
        }
        naviView.addSubview(backItem)
        naviView.addSubview(titleView)
        naviView.addSubview(skipItem)
        self.view.addSubview(naviView)
        backItem.snp.makeConstraints { make in
            make.left.equalTo(5.0)
            make.width.height.equalTo(50.0)
            make.bottom.equalTo(-3.0)
        }
        titleView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(backItem)
        }
        skipItem.snp.makeConstraints { make in
            make.centerY.equalTo(titleView)
            make.right.equalTo(-16.0)
            make.width.equalTo(conversionWithScrennWidth(value: 48.0))
            make.height.equalTo(24.0)
        }
        naviView.snp.makeConstraints { make in
            make.centerX.top.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(PanNavibarH)
        }
    }
}
